<?php
// 本文档自动生成，仅供测试运行
class BlogAction extends ColorBabyCommonAction
{

	public function index(){
		
		$this->getIndexCssStyle("yebk") ;
		$queryConditon ['type']= 'blog';
		$this->getCommonBlogQuery('jzpj',$queryConditon,1);
	}

	/**
	 * 写新文章和写新评价的初始化函数
	 * Enter description here ...
	 */
	public function add(){
		if(Session::is_set(C('USER_AUTH_KEY'))){
			//设置了类型，标明是写新文章还是写新评价
			//新文章 blog  新评价appraise
			$this->assign("type",$_REQUEST ['type']);
			$this->display();
		}else{
			$this->redirectWhenNotLogin();	
		}			
	}


	/**
	 * 保存blog评论
	 * Enter description here ...
	 */
	public function saveBlogComment(){
		$id = $_REQUEST ['id'];
		$content = $_REQUEST ['content'];
		$model = M ( "blog_comment" );
		$data ["blog_id"] = $id;
		$data ["content"] = $content ;
		$data["comment_user"] =  Session::get(C('USER_AUTH_KEY')) ;
		$data ["createdate"] = date('Y-m-d H:i:s',time());

		 
		$result = $model->add($data);
		 
		$msg = ""  ;
		 
		if ($result==false){
			$msg = "新增评论失败！" . $model->getError();
		}else{
			$msg = "新增评论成功！";
		}
		$this->ajaxReturn ( "111111", $msg, 1 );
	}


	public function showDetail(){		
		$model = M ( "Blog" );
		$comment_model = M ("blog_comment") ;
		$id = $_REQUEST ['id'];
		$condition['id'] = $id;
		$comment_condition['blog_id'] = $id;

		// 查询数据
		$list = $model->where($condition)->find();

		$comment_list = $comment_model->where($comment_condition)->order('createdate desc')->select();


		//更新该blog的点击数
		$data['readtimes'] = $list['readtimes']+1;
		// 更新的条件
		$result = $model->where($condition)->save($data);
		//dump($list) ;
		//$user = Session::get(C('USER_AUTH_KEY')) ;
		$this->assign ( "info", $list );
		$this->assign ( "comment_list", $comment_list );
		//$this->assign ( "user", $user);
		$this->display() ;
	}




	/**
	 * 删除Blog
	 * Enter description here ...
	 */
	public function deleteBlog(){
		$result = $this->delete("blog", $_REQUEST ['id']) ;
		if($result){
			$this->assign("jumpUrl","__APP__/LoginUser/index/type/blog");
			$this->success("删除成功！") ;
		}else{
			$this->success($result) ;
		}
	}

	/**
	 * 编辑Blog
	 * Enter description here ...
	 */
	public function editBlog(){
		$id = $_REQUEST ['id'];
		$Dao = M("blog");
		$condition['id'] = $id;
		$list = $Dao->where($condition)->find() ;
		$this->assign('vo', $list);
		$this->assign('type', "edit");
		$this->display(APP_PATH.'/Tpl/default/Blog/add.html');
	}

	/**
	 * 更新Blog
	 * Enter description here ...
	 */
	public function update(){

		// 需要更新的数据
		$data['summary'] = $_REQUEST ['summary'];
		$data['content'] = $_REQUEST ['content'];
		$data['title'] = $_REQUEST ['title'];
		$data['commentright'] = $_REQUEST ['commentright'];
		// 更新的条件
		$condition['id'] = $_REQUEST ['id'];
		 
		$temp_string = $this->localUpdate("blog",$condition,$data) ;
		 
		$this->ajaxReturn ( "111111", $temp_string==true?"更新成功":"更新失败".$temp_string, 1 );
		 
	}

	public function insert(){
		$content = $_REQUEST ['content'];
		$model = M ( "Blog" );
		$data ["user"] = Session::get(C('USER_AUTH_KEY')) ;
		$data ["title"] = $_REQUEST ['title'];
		$data ["content"] = stripslashes($content) ;
		$data ["commentright"] = $_REQUEST ['commentright'];
		$data ["category"] = "" ;
		$data ["readtimes"] = 0;
		$data ["createdate"] = date('Y-m-d H:i:s',time());
		//$data ["status"] = $_REQUEST ['status'];
		$data ["status"] = 1;
		$data ["type"] = $_REQUEST['type'];
		$temp_string = $_REQUEST ['status']==1?"新增文章成功！":"保存文章成功！";

		$result = $model->add($data);
		if($result==false){
			$this->ajaxReturn ( "111111", $temp_string, 1 );
		}else{
			$this->ajaxReturn ( "111111", $temp_string, 1 );
		}
			
	}

	/**
	 +----------------------------------------------------------
	 * 默认操作
	 +----------------------------------------------------------
	 */
	/*
	 public function index()
	 {
	 $this->display(THINK_PATH.'/Tpl/Autoindex/hello.html');
	 }
	 */
	/**
	 +----------------------------------------------------------
	 * 探针模式
	 +----------------------------------------------------------
	 */
	public function checkEnv()
	{
		load('pointer',THINK_PATH.'/Tpl/Autoindex');//载入探针函数
		$env_table = check_env();//根据当前函数获取当前环境
		echo $env_table;
	}

}
?>